import ipcRenderer from "../../ipcRenderer/ipcRenderer";
import axios from "axios";
import store from "../../store";
import songLyric from "../../components/songLyric/index.vue";
import lyricPage from "../lyricPage/index.vue";
import { localStorage } from "../../request"
import { Message } from "element-ui";

export default {
    name: "main-page",

    data() {
        return {
            SearchValue: '',
            restaurants: [],
            timeout: null,
            activeNames: ['1'],
            count: 20,
            History_and_recommendations_show: false,
            hots: null,
            Historical_Search: null,
            search_result: null,
            lyric_OR_control: true,
            lyricPage_show: false,
            volume: 60, // 音量的取值范围在：0(无声)~1(最大声)之间。
            volumeShow: false,
            Favorite_Songs: null,
            allMatch: null,
            songIMGurl: ""
        };
    },

    components: {
        songLyric,
        lyricPage
    },

    computed: {
        My_percent: function () {
            return store.state.Counter.My_percent;
        },
        My_percent1: function () {
            return store.getters.My_percent;
        },
        song: function () {
            return store.state.Counter.song;
        },
        play_state: function () {
            return store.state.Counter.play_state;
        },
        reload_DB: function () {
            return store.state.Counter.reload_DB;
        },
        play_mode: function () {
            return store.state.Counter.play_mode;
        },
        currentTime: function () {
            return store.state.Counter.currentTime;
        },
        duration: function () {
            return store.state.Counter.duration;
        },
        currentEmail: function () {
            return store.state.Counter.currentEmail;
        },
    },

    watch: {
        reload_DB: function (after, before) {
            if (after) {
                localStorage.getItem("email:" + this.currentEmail).then((res) => {
                    let data = res.data.Favorite_Songs;
                    this.Favorite_Songs = data;
                });

                // 搜索记录
                localStorage.getItem("email:" + this.currentEmail).then((res) => {
                    let data = res.data.Historical_Search;
                    if (data.length === 0) {
                        this.Historical_Search = ["李连杰", "成龙", "洪金宝", "这个杀手不太冷，有点热，他跳进了水里"];
                    } else {
                        this.Historical_Search = data.splice(1);
                    }
                });
            }
        },
        song: function (after, before) {
            this.songIMGurl = after.picUrl || after.al.picUrl;
            this.songIMGurl += '?imageView=1&type=webp&thumbnail=246x0';
            console.log(this.songIMGurl);
        }
    },
    created() {
        localStorage.getItem("email:" + this.currentEmail).then((res) => {
            let data = res.data.Favorite_Songs;
            this.Favorite_Songs = data;
        });

        axios.get("https://apis.netstart.cn/music/search/hot").then((res) => {
            this.hots = res.data.result.hots;
        });

        // 搜索记录
        localStorage.getItem("email:" + this.currentEmail).then((res) => {
            let data = res.data.Historical_Search;
            this.Historical_Search = data.splice(1);
        });
    },

    mounted() {
    },

    methods: {
        handleCommand(command) {
            console.log('click on item ' + command);
            switch (command) {
                case "0":
                    this.$router.push("/");
                    ipcRenderer.logout();
                    break;
                case "666":
                    localStorage.setItem("email:123@qq.com",
                        {
                            userName: "zc",
                            passWord: "123456",
                            level: 0,
                            data: {
                                Favorite_Songs: [],
                                Favorite_SongsList: [],
                                Historical_Search: [],
                                Historical_Songs_List: [],
                            }
                        }
                    ).then(() => {
                        localStorage.getItem("email:123@qq.com").then((res) => {
                            console.log(res.userName, res.passWord);
                        });
                    });

                    break;
                default:
                    console.log("暂时这项无业务");
                    break;
            }
        },
        change_volume(volume) {
            store.commit('change_volume', (volume / 100));
        },
        change_drawer() {
            store.commit('change_drawer', true);
        },
        updata_lyricPageShow() {
            this.lyricPage_show = !this.lyricPage_show;
        },
        lyric_TO_control() {
            this.lyric_OR_control = !this.lyric_OR_control;
        },
        maximize() {
            console.log("maximize");
            ipcRenderer.maximize();
        },
        minimize() {
            console.log("minimize");
            ipcRenderer.minimize();
        },
        close() {
            console.log("close");
            ipcRenderer.close();
        },
        reload() {
            console.log("reload");
            // ipcRenderer.reload();
            this.$router.go(0);
            // location.reload();
        },
        back() {
            console.log("back");
            let location = this.$route.fullPath;
            if (location === "/home/home2") {
                // this.$router.go(0);
                Message.warning("已经是首页不可再回退！")
            } else {
                this.$router.go(-1);
            }
        },
        person() {
            console.log("person");
            // ipcRenderer.openNewWin();
        },
        // 回到乐库首页
        musicPage() {
            console.log("musicPage");
            this.$router.push("/home/home2")
        },
        search(ev) {
            // console.log(ev);
            console.log("search", this.SearchValue);

            axios.get(
                "https://apis.netstart.cn/music/search/suggest?keywords=" + this.SearchValue
            ).then((res) => {
                this.search_result = res.data.result;
                // console.log(res.data.result);
            });

            localStorage.getItem("email:" + this.currentEmail).then((res) => {
                let data = res.data.Historical_Search;
                let i = data.findIndex((item) => {
                    return item === this.SearchValue
                });
                if (i === -1) {
                    data.push(this.SearchValue);
                }
                localStorage.setItem("email:" + this.currentEmail,
                    {
                        ...res,
                        data: {
                            ...res.data,
                            Historical_Search: data
                        }
                    }).then(() => {
                        console.log("存入历史成功");
                        store.commit("need_reload_DB", true);
                    });
            });
        },
        text() {
            this.History_and_recommendations_show = !this.History_and_recommendations_show;
        },
        clickTOcurrentTime(e) {
            this.$parent.$refs.audio.currentTime = (e.offsetX / e.path[1].offsetWidth) * this.duration
        },
        text1(hot) {
            this.SearchValue = hot.first || hot;
            console.log(this.SearchValue);
        },
        change_volumeShow() {
            console.log("change_volumeShow");
            this.volumeShow = !this.volumeShow;
        },
        // 收藏
        like() {
            console.log("liiiiike", "mainPage");
            // 收藏歌曲
            localStorage.getItem("email:" + this.currentEmail).then((res) => {
                // console.log(res.data.Favorite_Songs);
                let data = res.data.Favorite_Songs;
                let index = data.findIndex((item) => {
                    return this.song.id === item.id;
                });
                if (index === -1) {
                    data.push(this.song);
                    this.Favorite_Songs = data;
                    localStorage.setItem("email:" + this.currentEmail,
                        {
                            ...res,
                            data: {
                                ...res.data,
                                Favorite_Songs: data
                            }
                        }).then(() => {
                            store.commit("need_reload_DB", true);
                        });

                } else {
                    data.splice(index, 1);
                    this.Favorite_Songs = data;
                    localStorage.setItem("email:" + this.currentEmail,
                        {
                            ...res,
                            data: {
                                ...res.data,
                                Favorite_Songs: data
                            }
                        }).then(() => {
                            store.commit("need_reload_DB", true);
                        });
                }
            });
        },
        return_like_color() {
            if (this.Favorite_Songs) {
                let index = this.Favorite_Songs.findIndex((item) => {
                    return item.id === this.song.id;
                });
                return index !== -1 ? "#ff0000" : "#ffffff";
            } else {
                return "#ffffff";
            }
        },
        // 上一首
        before() {
            store.commit("Switch_song", 3);
        },
        // 下一首
        after() {
            store.commit("Switch_song", 4);
        },
        mode(play_mode) {
            switch (play_mode) {
                case "0":
                    console.log(play_mode);
                    store.commit("upload_play_mode", "1");
                    break;
                case "1":
                    console.log(play_mode);
                    store.commit("upload_play_mode", "2");
                    break;
                case "2":
                    console.log(play_mode);
                    store.commit("upload_play_mode", "0");
                    break;
            }
        },
        play() {
            // 点击播放触发
            console.log("play");
            store.commit("change_play_state", true);
        },
        pause() {
            // 点击暂停触发
            console.log("pause");
            store.commit("change_play_state", false);
        },
        toSongList(item) {
            console.log(item);
            this.$router.push("/home/songlist/" + item.id);
        },
        toSinger(artist) {
            this.$router.push("/home/singer/" + artist.id);
        },
        toNewSong(song) {
            console.log(song);
            store.commit("change_song", song);
            store.commit("change_play_state", true);
        },
    },
};